﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ModularFixture.CApMFCompoundResGridRow
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EMPAPPBASELib;
using EMPMODELLib;
using EMPTYPELIBRARYLib;
using System;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ModularFixture;

internal class CApMFCompoundResGridRow : CApMFLocatableGridRow
{
  private string m_name;
  private bool m_isUnit;
  private bool m_isRoot;

  internal CApMFCompoundResGridRow(string name, int prototypeId, int instanceId, int loadedId)
    : base(prototypeId, instanceId, loadedId)
  {
    this.m_name = name;
    this.m_targetLibraryId = CApMFAdminUtils.m_targetLibraryId;
    this.SetIcon();
    this.SetIsUnit();
    this.m_isRoot = false;
  }

  internal override int SaveNodeToEms(EmpObjectKey parent)
  {
    this.ValidateInternalIds();
    IEmpAppItem objectItem;
    if ((this.m_prototypeId == 0 || this.m_isRoot && this.m_targetLibraryId != CApMFAdminUtils.m_targetLibraryId) && CApMFAdminUtils.CreateEmsObject("UnitConfigurationPrototype", CApMFAdminUtils.m_targetLibraryId, out objectItem))
    {
      this.m_prototypeId = objectItem.Key.objectId;
      CApMFAdminUtils.SetObjectName(this.m_prototypeId, this.m_name);
      this.SetIsUnit();
    }
    if (this.m_prototypeId > 0 && parent.objectId != CApMFAdminUtils.m_targetLibraryId && (this.m_instanceId == 0 || !CApMFAdminUtils.IsDescendant(parent.objectId, this.m_instanceId)))
    {
      EmpObjectKey empObjectKey;
      empObjectKey.objectId = this.m_prototypeId;
      if (CApMFAdminUtils.ConnectEmsObject(empObjectKey, parent))
        this.m_instanceId = CApMFAdminUtils.GetInstanceId(parent, empObjectKey);
    }
    this.SaveAttributes();
    this.m_targetLibraryId = CApMFAdminUtils.m_targetLibraryId;
    return this.m_prototypeId;
  }

  private void SetIcon() => this.m_icon = CApMFAdminUtils.GetCompoundResourceImage();

  private void SetIsUnit()
  {
    this.m_isUnit = false;
    if (this.m_prototypeId <= 0)
      return;
    EmpContext empContext;
    empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
    IEmpObject iempObject = (IEmpObject) new EmpObjectClass();
    EmpObjectKey empObjectKey = new EmpObjectKey();
    empObjectKey.objectId = this.m_prototypeId;
    try
    {
      EmpFieldInfo field = iempObject.GetField(ref empContext, ref empObjectKey, "ConfigurationType");
      if (field.Value == null || !((string) field.Value).Equals("Unit"))
        return;
      this.m_isUnit = true;
    }
    catch (Exception ex)
    {
    }
  }

  internal override string Name
  {
    get => this.m_name;
    set => this.m_name = value;
  }

  internal bool IsUnit => this.m_isUnit;

  internal bool IsRoot
  {
    get => this.m_isRoot;
    set => this.m_isRoot = value;
  }
}
